package org.yxing.palpalgps.activity;

import org.yxing.palpalgps.AbstractActivity;
import org.yxing.palpalgps.R;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MEditText extends AbstractActivity implements View.OnClickListener, TextWatcher {
    
    private Button backButton;
    private Button saveButton;
    private TextView mEditText;
    private Toast toast;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edittext);

        Intent intent = getIntent();
        this.setTitle(getString(intent.getIntExtra("title", R.string.edit_default)));
        toast = Toast.makeText(this, getString(R.string.input_text_too_long), Toast.LENGTH_LONG);
        
        backButton = (Button) findViewById(R.id.back);
        saveButton = (Button) findViewById(R.id.save);
        mEditText = (EditText) findViewById(R.id.editText1);
        mEditText.setText(intent.getStringExtra("edit_text"));
        
        backButton.setOnClickListener(this);
        saveButton.setOnClickListener(this);
        mEditText.addTextChangedListener(this);
    }
    
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.save:
            Intent intent = new Intent();
            intent.putExtra("edit_text", mEditText.getText().toString());
            setResult(RESULT_OK, intent);
            finish();
            break;
        default:
            finish();
        }
    }
    
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        
    }
    
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        
    }
    
    public void afterTextChanged(Editable s) {
        int i = mEditText.getText().toString().length();
        if (i > 20) {
            toast.cancel();
            toast.show();
        }
    }
}
